<template>
  <div>
    <!-- 搜速区 -->
    <div class="search">
      <el-button
        type="primary"
        icon="el-icon-document-add"
        @click="addClick"
      ></el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="tableData" style="width: 99%" size="mini">
      <el-table-column label="角色编号" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.roleId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="角色名称" width="180">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.roleName }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="warning"
            @click="handleEdit(scope.$index, scope.row)"
            ><i class="el-icon-edit-outline"></i
          ></el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            ><i class="el-icon-delete"></i
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加抽屉 -->
    <el-drawer
      :title="isAdd ? '添加角色' : '修改角色'"
      :visible.sync="drawer"
      direction="rtl"
      :before-close="drawerClose"
    >
      <div style="padding: 0 15px">
        <el-form
          size="mini"
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="75px"
          class="demo-ruleForm"
        >
          <el-form-item label="角色名称" prop="roleName">
            <el-input
              type="text"
              v-model="ruleForm.roleName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">{{
              isAdd ? "添加" : "修改"
            }}</el-button>
            <el-button @click="resetForm('ruleForm')">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>
<script>
// 导入角色api
import * as role_api from "../../api/role_api";
export default {
  data() {
    // 验证角色
    var validateRoleName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入角色名称"));
      } else {
        callback();
      }
    };
    return {
      // 表格数据
      tableData: [],
      // 是否做添加操作
      isAdd: true,
      // 是否打开抽屉
      drawer: false,
      // 表单数据
      ruleForm: {
        // 登录名
        roleName: "",
      },
      // 表单验证数据
      rules: {
        // 验证账号
        roleName: [{ validator: validateRoleName, trigger: "blur" }],
      },
    };
  },
  created() {
    // 调用获取表格数据的方法
    this.getTableData();
  },
  methods: {
    // 获取表格数据
    async getTableData() {
      // let res = await this.$get("/role/list");
      // 通过api层调用接口获取数据
      let res = await role_api.getRoleList();
      this.tableData = res.data;
    },
    // 修改表格数据
    async handleEdit(index, row) {
       // 表示这是修改操作
      this.isAdd = false;
      // 打开抽屉
      this.drawer = true;
      
      let roleId = row.roleId;
      // 根据角色编号,获取角色对象
      let res = await this.$get("/role/getOne", { roleId });
      this.ruleForm = res;
    },
    // 删除表格数据
    async handleDelete(index, row) {
      await this.$con_f("是否确定删除?");
      let roleId = row.roleId;
      // 删除对应的角色信息
      let { success, message } = await this.$post("/role/delete", { roleId });
      if (success) {
        this.$message({
          showClose: true,
          message: message,
          type: "success",
          duration: 1500,
        });
        // 获取最新数据
        this.getTableData();
      } else {
        this.$message({
          showClose: true,
          message: message,
          type: "error",
          duration: 1500,
        });
      }
    },
    // 添加按钮点击方法
    addClick() {
      this.drawer = true;
    },
    // 关闭抽屉
    drawerClose(done) {
      // 清空表单数据
      this.resetForm("ruleForm");
      // 表单恢复成默认状态
      this.isAdd = true;
      done();
    },
    // 表单提交方法
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        // 判断表单数据是否验证成功
        if (valid) {
          if (this.isAdd) {
            // 执行添加功能
            let { success, message } = await this.$post(
              "/role/add",
              this.ruleForm
            );
            // 判断是否添加成功
            if (success) {
              this.$message({
                showClose: true,
                message: message,
                type: "success",
                duration: 1500,
              });
              // 清空表单
              this.resetForm("ruleForm");
              // 关闭抽屉
              this.drawer = false;
              // 获取最新数据
              this.getTableData();
            } else {
              this.$message({
                showClose: true,
                message: message,
                type: "error",
                duration: 1500,
              });
            }
          } else {
            // 执行修改功能
            let { success, message } = await this.$post(
              "/role/update",
              this.ruleForm
            );
            // 判断是否修改成功
            if (success) {
              this.$message({
                showClose: true,
                message: message,
                type: "success",
                duration: 1500,
              });
              this.drawer = false;
              // 获取最新数据
              this.getTableData();
            } else {
              this.$message({
                showClose: true,
                message: message,
                type: "error",
                duration: 1500,
              });
            }
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 表单重置方法
    resetForm(formName) {
      //重置表单
      this.$refs[formName].resetFields();
      // 清空数据
      this.ruleForm = {
        // 登录名
        roleName: "",
      };
    },
  },
};
</script>
<style scoped lang='scss'>
.search {
  padding: 4px 10px;
}
</style>